<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <!-- 按钮区域 -->
      <el-button type="primary" @click="addCate">添加分类</el-button>
      <!-- 表格区域 -->
      <!-- element自带的表格树形组件 -->
      <el-table
        :data="cateList"
        style="width: 100%; margin-bottom: 20px"
        row-key="cat_id"
        border
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="cat_name" label="分类名称"> </el-table-column>
        <el-table-column prop="cat_deleted" label="是否有效">
          <template v-slot="scope">
            <i
              :class="
                scope.row.cat_deleted ? 'el-icon-success' : 'el-icon-error'
              "
            ></i>
          </template>
        </el-table-column>
        <el-table-column prop="cat_level" label="排序">
          <template v-slot="scope">
            <el-tag v-if="scope.row.cat_level === 0">一级</el-tag>
            <el-tag
              style="margin-left: 10px"
              type="success"
              v-if="scope.row.cat_level === 1"
              >二级</el-tag
            >
            <el-tag
              style="margin-left: 20px"
              type="warning"
              v-if="scope.row.cat_level === 2"
              >三级</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template>
            <!-- 编辑按钮 -->
            <el-button type="primary" size="mini" icon="el-icon-edit"
              >编辑</el-button
            >
            <!-- 删除按钮 -->
            <el-button type="danger" size="mini" icon="el-icon-delete"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[3, 5, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 添加按钮弹出的对话框 -->
    <el-dialog
      title="添加分类"
      :visible.sync="addCateDialogVisible"
      width="50%"
    >
      <!-- 分类名称验证输入 -->
      <el-form
        :model="addCateList"
        :rules="addCateListRules"
        ref="addCateListRef"
        label-width="100px"
      >
        <el-form-item label="分类名称：" prop="cat_name">
          <el-input v-model="addCateList.cat_name"></el-input>
        </el-form-item>
        <!-- 分类分级下拉菜单 -->
        <el-form-item label="父级分类：">
          <el-cascader
            v-model="value"
            :options="parentCatelist"
            :props="{
              expandTrigger: 'hover',
              value: 'cat_id',
              label: 'cat_name',
              children: 'children'
            }"
            @change="handleChange"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCateSure">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      value: [],
      addCateListRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { min: 2, max: 5, message: '用户名的长度在2到5之间' }
        ]
      },
      // 添加分类的请求参数
      addCateList: {
        cat_pid: 0,
        cat_name: '',
        cat_level: 0
      },
      // 商品分类数据列表
      cateList: [],
      // 总数据条数
      total: 0,
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      addCateDialogVisible: false,
      parentCatelist: []
    }
  },
  methods: {
    // 获取商品分类数据列表
    async getGoodsList() {
      const { data: res } = await this.$http.get('categories', this.queryInfo)
      if (res.meta.status !== 200) return this.$message.info(res.meta.msg)
      this.cateList = res.data
      this.total = res.data.length
      console.log(res.data)
    },
    // 监听pagesize改变事件
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    // 监听pagenum改变
    handleCurrentChange(newNum) {
      this.queryInfo.pagenum = newNum
      this.getGoodsList()
    },
    // 添加按钮
    addCate() {
      this.getParentCatelist()
      this.addCateDialogVisible = !this.addCateDialogVisible
    },
    async addCateSure() {
      const { data: res } = await this.$http.post(
        'categories',
        this.addCateList
      )
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.getGoodsList()
    },
    // 级联菜单
    handleChange() {
      console.log(1)
    },
    // 点击添加分类按钮之后，要获取到前两级的分类列表
    async getParentCatelist() {
      const { data: res } = await this.$http.get('categories', {
        params: { type: 2 }
      })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.parentCatelist = res.data
    }
  },
  created() {
    // 获取商品分类数据列表
    this.getGoodsList()
  }
}
</script>

<style lang="less" scoped>
.el-icon-success {
  color: green;
}
.el-icon-error {
  color: red;
}
</style>
